const { addOrderserves, getorderservice, getproductdata, setorderservice } = require('../service/order')
module.exports = {
    async addorder(ctx, next) {
        const { address, allprice, bz, canju, issong, kw, productid, sum, userid } = ctx.request.body
        const orderid = (Math.floor(Math.random() * 9999) + Math.floor(Math.random() * 9999) + new Date().getTime()).toString()
        const adddata = {
            address, allprice, bz, canju, issong, kw, productid, sum, userid, orderid
        }
        const rudata = await addOrderserves(adddata)
        ctx.body = {
            code: 200,
            msg: '添加成功',
            data: rudata
        }
    },
    async getorder(ctx, next) {
        const { userid } = ctx.request.body
        const orderdata = await getorderservice(userid)
        // orderdata.forEach(async (e, index) => {
        //     getproductdata(e.productid).then(data => {
        //         newdata.push({
        //             ...e,
        //             productdata: data
        //         })
        //         if (index == orderdata.length - 1) {
        //             console.log(newdata)
        //         }
        //     })
        // })

        ctx.body = {
            code: 200,
            msg: '查询成功',
            data: orderdata
        }
    },
    async setorder(ctx, next) {
        const { orderid, status } = ctx.request.body
        const setdata = await setorderservice(orderid, status)
        return ctx.body = {
            code: 200,
            msg: '修改成功',
        }
    }
}